<template>
    <div class="verify-code-page">
        <div class="header">
            <h1>输入验证码</h1>
        </div>

        <div class="info">
            <p>
                验证码已发送到
                <strong :style="{ fontSize: 'larger' }">{{ phoneNumber }}</strong>
                ，15分钟内填写有效。
            </p>
        </div>

        <div class="code-inputs">
            <!-- <van-password-input
                v-model="enteredCode"
                :length="4"
                :mask="false"
                @focus="showKeyboard = true"
                @blur="showKeyboard = false"
                @complete="onCodeComplete" /> -->
            <!-- :gutter="10" -->
            <van-password-input
                :value="code"
                info="密码为 4 位数字"
                :length="4"
                error-info="密码错误"
                :focused="showKeyboard"
                @focus="showKeyboard = true" />
            <van-number-keyboard
                v-model="code"
                :show="true" />
        </div>

        <div class="resend">
            <van-button
                :disabled="!canResend"
                @click="handleResend"
                size="small">
                {{ resendButtonText }}
            </van-button>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            code: '12',
            phoneNumber: '', // 假设通过路由参数传递过来
            verificationCodeInputFields: Array.from({ length: 4 }, () => ({ value: '' })),
            resendCountdown: 60,
            resendTimerId: null,
            canResend: false,
            enteredCode: '',
            showKeyboard: true,
        };
    },
    created() {
        this.startResendCountdown();
        this.showKeyboard = true; // 保证页面加载时直接调起数字键盘
    },
    beforeDestroy() {
        this.clearResendTimer();
    },
    methods: {
        startResendCountdown() {
            this.resendCountdown = 60;
            this.canResend = false;
            this.resendTimerId = setInterval(() => {
                this.resendCountdown -= 1;
                this.canResend = this.resendCountdown === 0;
                this.$forceUpdate(); // 强制更新以实时显示倒计时
            }, 1000);
        },
        clearResendTimer() {
            clearInterval(this.resendTimerId);
        },
        onCodeComplete(code) {
            this.enteredCode = code;
            // 这里可以进行验证码校验逻辑，如向服务器发送请求验证验证码是否正确
        },
        handleResend() {
            if (!this.canResend) return;
            // 这里模拟重新发送验证码的逻辑，实际项目中请替换为实际的发送逻辑
            this.startResendCountdown();
        },
    },
    computed: {
        resendButtonText() {
            return this.resendCountdown > 0 ? `重新发送(${this.resendCountdown}s)` : '重新发送';
        },
    },
};
</script>

<style scoped>
.verify-code-page {
    text-align: center;
}

.header h1 {
    font-size: 2.5rem;
}

.info p {
    font-size: 1.2rem;
    margin-bottom: 1rem;
}

.info strong {
    font-size: larger;
}

.code-inputs {
    /* display: flex; */
    /* justify-content: space-between; */
    /* margin-bottom: 1rem; */
}

.code-inputs .van-field {
    flex: 0 0 calc(25% - ¼rem);
    max-width: calc(25% - ¼rem);
    margin: 0 ¼rem;
    height: 3rem;
    line-height: 3rem;
    text-align: center;
}

.resend {
    margin-top: 1rem;
}
</style>
